﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static LotusIslandOrderProcessing.model.Save;

namespace LotusIslandOrderProcessing.PLCMake
{
    public class PlcMakes
    {
        public List<object> list = new List<object>();
        public string lists = "";
        public bool readBol = true;
        private static PlcMakes instance;
        private PlcMakes() { }

        public static PlcMakes Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PlcMakes();
                }
                return instance;
            }
        }
        public void Init()
        {
            try
            {
                for (int i = 0; i < statelist.Instance.state.Count; i++)
                {
                    PlcList plc = new PlcList();
                    plc.var = statelist.Instance.state[i];
                    PlcModel model = new PlcModel();
                    model.paramss = plc;
                    model.method = "PlcProgram.Read";
                    model.id = i;
                    list.Add(model);
                }
                lists = JsonConvert.SerializeObject(list);
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        public void MTRead()
        {
            try
            {
                var readlist = HttpClient.Instance.HttpPostRaw("", lists, HttpClient.url_start, true);
                //[["jsonrpc":"2.0","id":1,"result":0),
                if (readlist != "")
                {
                    var sss = JsonConvert.DeserializeObject<List<object>>(readlist);
                    if (sss.Count == 17)
                    {
                        for (int i = 0; i < sss.Count; i++)
                        {
                            try
                            {
                                var read = JsonConvert.DeserializeObject<PlcRead>(sss[i].ToString());
                                if (read != null)
                                {
                                    ReadBol(read.id, read.result);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                        if (MtBol)
                        {
                            TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                            testDelegate.BeginInvoke(null, null);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke();
        private void DelegateInvokeMethod()
        {
            try
            {
                saveDeviceMatter save = new saveDeviceMatter();
                save.matterStatusJson = JsonConvert.SerializeObject(CoffeeMatters.Instance);
                save.deviceStatusJson = JsonConvert.SerializeObject(deviceStatus.Instance);
                var saves = JsonConvert.SerializeObject(save);
                HttpMsg.Instance.saveDeviceMatterMsg(saves);

            }
            catch (Exception)
            {
                return;
            }
        }
        public bool MtBol = false;
        public void ReadBol(int id, string result)
        {
            try
            {

                switch (id)
                {
                    case 0:
                        PlcReadMsg.Instance.Print3 = int.TryParse(result, out var print3) ? print3 : 0;
                        PlcReadBol.Instance.Print3 = (int.TryParse(result, out var print31) ? print31 : 0) == 0;
                        break;
                    case 1:
                        PlcReadMsg.Instance.Print4 = int.TryParse(result, out var print4) ? print4 : 0;
                        PlcReadBol.Instance.Print4 = (int.TryParse(result, out var print41) ? print41 : 0) == 0;
                        break;
                    case 2:
                        PlcReadMsg.Instance.Making3 = int.TryParse(result, out var making3) ? making3 : 0;
                        break;
                    case 3:
                        PlcReadMsg.Instance.Making4 = int.TryParse(result, out var making4) ? making4 : 0;
                        break;
                    case 4:
                        PlcReadMsg.Instance.Order_Number3 = result;
                        if (result != "")
                        {
                            if (MainWindow.makeModel1s.Any(a => a.Guid == result && a.Type == 0))
                            {
                                var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == result);
                                model.Type = 1;
                            }
                        }
                        break;
                    case 5:
                        PlcReadMsg.Instance.Order_Number4 = result;
                        if (result != "")
                        {
                            if (MainWindow.makeModel1s.Any(a => a.Guid == result && a.Type == 0))
                            {
                                var model = MainWindow.makeModel1s.FirstOrDefault(a => a.Guid == result);
                                model.Type = 1;
                            }
                        }
                        break;
                    case 6:
                        PlcReadMsg.Instance.Matter1 = int.TryParse(result, out var matter1) ? matter1 : 0;
                        PlcReadBol.Instance.Matter1 = (int.TryParse(result, out var matter011) ? matter011 : 0) == 1;
                        if (CoffeeMatters.Instance.IslandMatter2 == PlcReadMsg.Instance.Matter1)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter2 = PlcReadMsg.Instance.Matter1 == 1 ? 0 : 1;
                        break;
                    case 7:
                        PlcReadMsg.Instance.Matter5 = int.TryParse(result, out var matter5) ? matter5 : 0;
                        PlcReadBol.Instance.Matter5 = (int.TryParse(result, out var matter51) ? matter51 : 0) == 0;
                        //if (CoffeeMatters.Instance.MilkMatter11 == PlcReadMsg.Instance.Matter5)
                        //{
                        //    MtBol = true;
                        //}
                        //CoffeeMatters.Instance.MilkMatter11 = PlcReadMsg.Instance.Matter5 == 1 ? 0 : 1;
                        break;
                    case 8:
                        PlcReadMsg.Instance.Matter8 = int.TryParse(result, out var matter8) ? matter8 : 0;
                        PlcReadBol.Instance.Matter8 = (int.TryParse(result, out var matter81) ? matter81 : 0) == 0;
                        //if (CoffeeMatters.Instance.IslandMatter2 == PlcReadMsg.Instance.Matter8)
                        //{
                        //    MtBol = true;
                        //}
                        //CoffeeMatters.Instance.MilkMatter7 = PlcReadMsg.Instance.Matter8 == 1 ? 0 : 1;
                        break;
                    case 9:
                        PlcReadMsg.Instance.Matter9 = int.TryParse(result, out var matter9) ? matter9 : 0;
                        PlcReadBol.Instance.Matter9 = (int.TryParse(result, out var matter91) ? matter91 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter44 == PlcReadMsg.Instance.Matter9)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter44 = PlcReadMsg.Instance.Matter9 == 1 ? 0 : 1;
                        break;
                    case 10:
                        PlcReadMsg.Instance.Matter10 = int.TryParse(result, out var matter10) ? matter10 : 0;
                        PlcReadBol.Instance.Matter10 = (int.TryParse(result, out var matter101) ? matter101 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter53 == PlcReadMsg.Instance.Matter10)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter53 = PlcReadMsg.Instance.Matter10 == 1 ? 0 : 1;
                        break;
                    case 11:
                        PlcReadMsg.Instance.Matter11 = int.TryParse(result, out var matter11) ? matter11 : 0;
                        PlcReadBol.Instance.Matter11 = (int.TryParse(result, out var matter111) ? matter111 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter52 == PlcReadMsg.Instance.Matter11)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter52 = PlcReadMsg.Instance.Matter11 == 1 ? 0 : 1;
                        break;
                    case 12:
                        PlcReadMsg.Instance.Matter12 = int.TryParse(result, out var matter12) ? matter12 : 0;
                        PlcReadBol.Instance.Matter12 = (int.TryParse(result, out var matter121) ? matter121 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter43 == PlcReadMsg.Instance.Matter12)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter43 = PlcReadMsg.Instance.Matter12 == 1 ? 0 : 1;
                        break;
                    case 13:
                        PlcReadMsg.Instance.Matter13 = int.TryParse(result, out var matter13) ? matter13 : 0;
                        PlcReadBol.Instance.Matter13 = (int.TryParse(result, out var matter131) ? matter131 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter47 == PlcReadMsg.Instance.Matter13)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter47 = PlcReadMsg.Instance.Matter13 == 1 ? 0 : 1;
                        break;
                    case 14:
                        PlcReadMsg.Instance.Matter14 = int.TryParse(result, out var matter14) ? matter14 : 0;
                        PlcReadBol.Instance.Matter14 = (int.TryParse(result, out var matter141) ? matter141 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter45 == PlcReadMsg.Instance.Matter14)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter45 = PlcReadMsg.Instance.Matter14 == 1 ? 0 : 1;
                        break;
                    case 15:
                        PlcReadMsg.Instance.Fault0 = int.TryParse(result, out var fault0) ? fault0 : 0;
                        PlcReadBol.Instance.Fault0 = (int.TryParse(result, out var fault01) ? fault01 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter23 == PlcReadMsg.Instance.Fault0)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter23 = PlcReadMsg.Instance.Fault0 == 1 ? 0 : 1;
                        break;
                    case 16:
                        PlcReadMsg.Instance.Fault1 = int.TryParse(result, out var Fault1) ? Fault1 : 0;
                        PlcReadBol.Instance.Fault1 = (int.TryParse(result, out var Fault11) ? Fault11 : 0) == 0;
                        if (CoffeeMatters.Instance.IslandMatter27 == PlcReadMsg.Instance.Fault1)
                        {
                            MtBol = true;
                        }
                        CoffeeMatters.Instance.IslandMatter27 = PlcReadMsg.Instance.Fault1 == 1 ? 0 : 1;
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void SelRead()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (readBol)
                        {
                            MTRead();
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(1500);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
